package com.xmy.cultivate.mapper;

import com.xmy.cultivate.entity.Permission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author baomidou
 * @since 2022-08-07
 */
public interface PermissionMapper extends BaseMapper<Permission> {


    /**
     * 获取用户权限
     * @param adminId
     * @param type
     * @return
     */
    @Select({"<script>",
            "SELECT",
            "code",
            "FROM",
            "permission ",
            "WHERE",
            "(",
            "id IN (",
            "SELECT",
            "permission_id ",
            "FROM",
            "role_permission ",
            "WHERE",
            "role_id IN ( SELECT role_id FROM role_admin WHERE admin_id = ${adminId} )) ",
            "OR id IN ( SELECT permission_id FROM staff_permission WHERE staff_id = ${adminId} AND type = 1 )) ",
            "AND id NOT IN ( SELECT permission_id FROM staff_permission WHERE staff_id = ${adminId} AND type = 2 ) ",
            "GROUP BY",
            "type,",
            "`code`",
            "</script>"})
    public List<String> getPermissionCodeList(@Param("adminId") Long adminId,@Param("type") Integer type);


    @Select({"<script>",
            "select `code` from permission where code &lt;&gt; '' and code  is not null",
            "<when test='type!=null and type!=&apos;&apos;'>",
            "and type = ${type}",
            "</when>",
            "</script>"})
    public List<String> getPermissionCodeAll(@Param("type") Integer type);

    /**
     * 获取用户权限
     * @param adminId
     * @param type
     * @return
     */
    @Select({"<script>",
            "SELECT",
                    "code",
                    "FROM",
                    "permission ",
                    "WHERE",
                    "(",
                      "id IN (",
                      "SELECT",
                      "permission_id ",
                      "FROM",
                      "role_permission ",
                      "WHERE",
                      "role_id IN ( SELECT role_id FROM role_admin WHERE admin_id = ${adminId} )) ",
                    "OR id IN ( SELECT permission_id FROM staff_permission WHERE staff_id = ${adminId} AND type = 1 )) ",
                    "AND id NOT IN ( SELECT permission_id FROM staff_permission WHERE staff_id = ${adminId} AND type = 2 ) ",
                    "and `code` &lt;&gt; '' and `code` is not null",
                    "GROUP BY",
                    "type,",
                    "`code`",
            "</script>"})
    public Set<String> getPermissoinCodeForAdmin(@Param("adminId") Long adminId, @Param("type") Integer type);

}
